//
//  UILabel+VariableHeight.m
//  HSBCHybrid
//
//  Created by gltc on 7/17/13.
//
//

#import "UILabel+VariableHeight.h"

@implementation UILabel (VariableHeight)

- (CGFloat)calculateHeightWithMaxHeight:(NSUInteger)maxHeight
{
    // check param
    if (maxHeight <= 0) {
        const NSUInteger kDefaultMaxHeight = 416; // one page
        maxHeight = kDefaultMaxHeight;
    }
    
    // check text
    if (self.text.length == 0) {
        return 0.0;
    }
    
    // calculate fit height.
    
    CGRect labelFrame  = self.frame;
    CGSize contentMaxSize = CGSizeMake(labelFrame.size.width, maxHeight);
    
    UIFont *labelFont  = self.font;
    if (labelFont == nil) {
        // default font
        labelFont = [UIFont systemFontOfSize:[UIFont systemFontSize]];
    }
    
    CGSize contentSize = [self.text sizeWithFont:labelFont constrainedToSize:contentMaxSize lineBreakMode:self.lineBreakMode];
        
    return contentSize.height;
}

@end
